﻿using System;
using AnyCAD.Foundation;

namespace Rapid.Sketch.Plugin.Model
{
    /// <summary>
    /// Model
    /// </summary>
    public class SectionBarModel : ElementModel
    {

        public double InnerRadius = 100;
        public double Thickness = 10;
        public double Length = 1000;

        public SectionBarModel()
        {

        }

        public TopoShape CreateShape()
        {
            return ShapeBuilder.MakeTube(GP.Origin(), GP.DZ(), InnerRadius, Thickness, Length);
        }

        static public Element NewInstance(Document doc, String name)
        {
            var schema = ElementSchemaManager.Instance().FindSchema(nameof(SectionBarModel));
            var instance = schema.CreateEntity(name, doc);
            var component = schema.AddComponent(instance.GetId(), doc);
            return instance;
        }
    }

    /// <summary>
    /// Schema
    /// </summary>
    public class SectionBarSchema : ElementSchema
    {
        public SectionBarSchema()
            :base(nameof(SectionBarModel))
        {
            SetIcon("/Rapid.Common.Res;component/Image/SectionBar.png");
        }

        // 创建存储数据的类型
        public override string OnGetEntityClassName()
        {
            return nameof(ShapeElement);
        }

        public override ElementModel CreateModel()
        {
            return new SectionBarModel();
        }

        //自定义属性变化，更新几何形状
        public override bool OnParameterChanged(Document doc, SchemaComponent component)
        {
            var model = new SectionBarModel();
            model.Load(component);

            var shape = model.CreateShape();
            if (shape == null)
                return false;

            var shapeElement = ShapeElement.Cast(component.GetEntity());
            if (shapeElement == null)
                return false;
            shapeElement.SetShape(shape);
            return true;
        }


    }
}
